package vn.vietel.pms.web.action;

import java.util.LinkedList;
import java.util.List;

import javax.faces.event.ActionEvent;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

import vn.vietel.pms.entity.VTypeOfProperty;
import vn.vietel.pms.service.TypeOfPropertyService;

@Scope(ScopeType.SESSION)
@Name("typeOfProperty")
public class TypeOfPropertyAction {
	@In(required = false)
	@Out(required = false)
	@DataModelSelection("items")
	private VTypeOfProperty vtypeofproperty;

	@Logger
	Log log;
	@In("#{typeOfPropertyService}")
	private TypeOfPropertyService typeOfPropertyService;

	@In
	private FacesMessages facesMessages;

	@DataModel
	private List<VTypeOfProperty> items;

	private Long id;

	private boolean editCompleted;

	private boolean editting;

	private String action;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
		editCompleted = false;
		editting = false;
		if (id != null) {
			vtypeofproperty = typeOfPropertyService.find(id);
			editting = (vtypeofproperty != null);
		} else {
			vtypeofproperty = new VTypeOfProperty();
			editting = false;
		}
	}

	public void createItem() {
		try {
			editCompleted = false;
			typeOfPropertyService.create(vtypeofproperty);
			getItems();
			editCompleted = true;
		} catch (IllegalStateException e) {
			facesMessages.add("Opps!!! Could not insert....");
			facesMessages.add(e.getMessage());
			editCompleted = false;
		}
	}

	public void updateItem() {
		try {
			editCompleted = false;
			typeOfPropertyService.update(vtypeofproperty);
			getItems();
			editCompleted = true;
			editting = false;

		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	public void deleteItem(ActionEvent ae) {
		try {
			Long rId = vtypeofproperty.getId();
			VTypeOfProperty rgp = typeOfPropertyService.find(rId);
			typeOfPropertyService.delete(rgp);
			getItems();
		} catch (IllegalStateException e) {
			facesMessages.add(e.getMessage());
		}
	}

	@Factory()
	public List<VTypeOfProperty> getItems() {
		items = typeOfPropertyService.getAll();
		if (items == null)
			items = new LinkedList<VTypeOfProperty>();
		return items;
	}

	public void setItems(List<VTypeOfProperty> items) {
		this.items = items;
	}

	public boolean isEditCompleted() {
		return editCompleted;
	}

	public void setEditCompleted(boolean editcompleted) {
		this.editCompleted = editcompleted;
	}

	public boolean isEditting() {
		return editting;
	}

	public void setEditting(boolean editting) {
		this.editting = editting;
	}

	public boolean notEditing() {
		return !editting;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

}
